package com.java1234.web;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.java1234.dao.MessageDao;
import com.java1234.model.Message;
import com.java1234.util.DbUtil;
import com.java1234.util.ImportExcelUtils;

import net.sf.json.JSONObject;

public class ImportMessageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
 

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ImportExcelUtils importE = new ImportExcelUtils();
		String path=getServletContext().getRealPath("upload");		
		  File fileDir=new File(path);
		  File currFile=null;
		  if(fileDir.isDirectory()){
		   File files[]=fileDir.listFiles();
		    currFile=files[0];		   

		   		  }
		 String fileName=currFile.getName();
		 
		//String fileUrl="D:\\eclipse-ee\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\MessageManage\\upload\\"+fileName;
		 String fileUrl=getServletContext().getRealPath("upload/"+fileName);
		List<List<String>> sheetList = importE.getContent(fileUrl);
		List<String> rowContents=null;
		List<String> rowContent1=null;
		//int i=0;
		/*String date = null;
		String cid = null;
		String choose = null;
		String name = null;
		String form = null;
		String head = null;
		String position = null;
		String workspace = null;
		String teleph = null;
		String members = null;
		String m_desc = null;*/
		Message message = new Message();
		JSONObject result=new JSONObject();
		rowContent1 = sheetList.get(1);		
		String date1 = "";
		String cid1 = "";
		String choose1 = "";
		String name1 = "";
		String form1 = "";
		String head1 = "";
		String position1 = "";
		String workspace1 = "";
		String teleph1 = "";
		String members1 = "";
		String m_desc1 = "";
		if(rowContent1.size()>=11){
			date1 = rowContent1.get(0);
			cid1 = rowContent1.get(1);
			choose1 = rowContent1.get(2);
			name1 = rowContent1.get(3);
			form1 = rowContent1.get(4);
			head1 = rowContent1.get(5);
			position1 = rowContent1.get(6);
			workspace1 = rowContent1.get(7);
			teleph1 = rowContent1.get(8);
			members1 = rowContent1.get(9);
			m_desc1 = rowContent1.get(10);
		}
		
		if(date1.equals("年份")&&cid1.equals("分类")&&choose1.equals("选题方向")
				&&name1.equals("项目名称")&&form1.equals("项目形式")
				&&head1.equals("项目负责人")&&position1.equals("职称")
				&&workspace1.equals("工作单位")&&teleph1.equals("联系方式")
				&&members1.equals("项目组成员")&&m_desc1.equals("备注")){
			for( int i = 2;i< sheetList.size(); i++ ){
				rowContents = sheetList.get(i);
				

					String date = rowContents.get(0);
					String cid = rowContents.get(1);
					String choose = rowContents.get(2);
					String name = rowContents.get(3);
					String form = rowContents.get(4);
					String head = rowContents.get(5);
					String position = rowContents.get(6);
					String workspace = rowContents.get(7);
					String teleph = rowContents.get(8);
					String members = rowContents.get(9);
					String m_desc = rowContents.get(10);

				
				
				message.setDate(date);
				message.setCid(cid);
				message.setChoose(choose);
				message.setForm(form);
				message.setHead(head);
				message.setPosition(position);
				message.setWorkspace(workspace);
				message.setTeleph(teleph);
				message.setMembers(members);
				message.setName(name);
				message.setM_desc(m_desc);
				//设置资助默认
				message.setAid("未资助");
				DbUtil dbUtil=new DbUtil();
				MessageDao messageDao=new MessageDao();
				Connection con=null;
				
				try{
					con=dbUtil.getCon();
					int saveNums=0;
					//result=new JSONObject();
						saveNums=messageDao.messageAdd(con, message);

					if(saveNums>0){
						//result.put("total", i-1);
						result.put("total", i-1);
						result.put("success", "true");
					}else{
						result.put("success", "true");
						result.put("errorMsg", "添加失败");
					}
				}catch(Exception e){
					e.printStackTrace();
					//response.sendRedirect(request.getContextPath()+"/succ.jsp");
					
				}finally{
					try {
						dbUtil.closeCon(con);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}else{
			result.put("total", -1);
		}
		File file = new File(fileUrl);
		
		file.delete();
		
		request.setAttribute("result", result);
		request.getRequestDispatcher("/messageManage.jsp").forward(request, response);
		//response.sendRedirect(request.getContextPath()+"/messageManage.jsp");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);
	}

}
